package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.po.RelayTimingParam;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设置继电器定时控制
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetRelayTiming implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 所在发送器的序号
         */
        private Integer prcIndex;
        /**
         * 所在网口的序号
         */
        private Integer portIndex;
        /**
         * 网口内的多功能卡序号
         */
        private Integer functionIndex;
        /**
         * 继电器定时控制参数
         */
        private List<RelayTimingParam> timingParam;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 所在发送器的序号
         */
        private Integer prcIndex;
        /**
         * 所在网口的序号
         */
        private Integer portIndex;
        /**
         * 网口内的多功能卡序号
         */
        private Integer functionIndex;
        /**
         * 返回所有继电器定时控制参数
         */
        private List<RelayTimingParam> timingParam;
    }

    private InputMsg input;
    private OutputMsg output;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetRelayTiming.InputMsg.Builder inputMsg =
                CompleteInstruction.SetRelayTiming.InputMsg.newBuilder();
        SetRelayTiming.InputMsg thisInput = this.getInput();
        inputMsg.setPrcIndex(thisInput.getPrcIndex());
        inputMsg.setPortIndex(thisInput.getPortIndex());
        inputMsg.setFunctionIndex(thisInput.getFunctionIndex());
        inputMsg.addAllTimingParam(thisInput.getTimingParam().stream().map(RelayTimingParam::toProto).collect(Collectors.toList()));
        final CompleteInstruction.SetRelayTiming.Builder builder =
                CompleteInstruction.SetRelayTiming.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetRelayTiming byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetRelayTiming setRelayTiming = null;
        try {
            setRelayTiming = CompleteInstruction.SetRelayTiming.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setRelayTiming)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetRelayTiming.OutputMsg outputMsg = new SetRelayTiming.OutputMsg();
        CompleteInstruction.SetRelayTiming.OutputMsg output = setRelayTiming.getOutput();
        List<RelayTimingParam> collect = output.getTimingParamList().stream()
                .map(e -> new RelayTimingParam().formProto(e)).collect(Collectors.toList());
        outputMsg.setPrcIndex(output.getPrcIndex());
        outputMsg.setPortIndex(output.getPortIndex());
        outputMsg.setFunctionIndex(output.getFunctionIndex());
        outputMsg.setTimingParam(collect);
        this.setOutput(outputMsg);
        return this;
    }
}

